﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tetris.Game.ControlsBase.Common;
using Tetris.Game.Properties;

namespace Tetris.Game.ControlsBase
{
    public class KeystrokeControl : Control
    {
        private System.Timers.Timer animationTimer;
        private int animationStep = 0;
        private int totalSteps = 10; // 总步数
        public KeystrokeControl()
        {
            Font = new Font("隶书", 9);
            ForeColor = Color.FromArgb(81, 81, 81);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            BackColor = Color.Transparent;

            animationTimer = new System.Timers.Timer();
            animationTimer.Interval = 10; // 每个步骤的间隔时间
            animationTimer.Elapsed += AnimationTimer_Elapsed; ;
        }

        private void AnimationTimer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
        {
            if (animationStep++ >= totalSteps)
            {
                animationTimer.Stop();
                AnimationColor1 = Color.FromArgb(206, 204, 205);
                AnimationColor2 = Color.FromArgb(239, 239, 241);

                OuterSpacingColor1 = Color.FromArgb(220, 220, 220);
                OuterSpacingColor2 = Color.FromArgb(94, 92, 92);
                this.ExecBeginInvoke(() =>
                {
                    Invalidate();
                });
            }
            else
            {
                AnimationColor1 = Color.FromArgb(254, 9, 44);
                AnimationColor2 = Color.FromArgb(192, 0, 0);

                OuterSpacingColor1 = Color.FromArgb(160, 207, 255);
                OuterSpacingColor2 = Color.FromArgb(64, 158, 255);

                this.ExecBeginInvoke(() =>
                {
                    Invalidate();
                });
            }
        }
        public void KeystrokeClick()
        {
            animationStep = 0; // 重置动画步骤
            animationTimer.Start(); // 启动定时器
        }
        private Color AnimationColor1 = Color.FromArgb(206, 204, 205);
        private Color AnimationColor2 = Color.FromArgb(239, 239, 241);

        private Color OuterSpacingColor1 = Color.FromArgb(220, 220, 220);
        private Color OuterSpacingColor2 = Color.FromArgb(94, 92, 92);


        private int _radius = 5;
        /// <summary>
        /// 圆角角度
        /// </summary>
        [Description("圆角角度"), Category("TF.UI")]
        [DefaultValue(5)]
        public int Radius
        {
            get
            {
                return _radius;
            }
            set
            {
                if (_radius != value)
                {
                    _radius = Math.Max(0, value);
                    Invalidate();
                }
            }
        }
        private int _imageSize = 10;
        [Description("图像大小"), Category("TF.UI")]
        [DefaultValue(10)]
        public int ImageSize
        {
            get => _imageSize;
            set
            {
                var temp = Math.Max(0, value);
                if (_imageSize == temp) return;
                _imageSize = temp;
                Invalidate();
            }
        }
        private KeystrokeType _keystrokeType = KeystrokeType.Up;
        [Description("按键类型"), Category("TF.UI")]
        [DefaultValue(KeystrokeType.Up)]
        public KeystrokeType Keystroke
        {
            get => _keystrokeType;
            set
            {
                if (_keystrokeType == value) return;
                _keystrokeType = value;
                Invalidate();
            }
        }
        private int _outerSpacing = 2;
        [Description("外边距"), Category("TF.UI")]
        [DefaultValue(2)]
        public int OuterSpacing
        {
            get => _outerSpacing;
            set
            {
                if (_outerSpacing == value) return;
                _outerSpacing = value;
                Invalidate();
            }
        }
        private int _innerSpacing = 2;
        [Description("内边距"), Category("TF.UI")]
        [DefaultValue(2)]
        public int InnerSpacing
        {
            get => _innerSpacing;
            set
            {
                if (_innerSpacing == value) return;
                _innerSpacing = value;
                Invalidate();
            }
        }
        private int _innerSquareWidth = 22;
        [Description("内正方形宽"), Category("TF.UI")]
        [DefaultValue(22)]
        public int InnerSquareWidth
        {
            get => _innerSquareWidth;
            set
            {
                if (_innerSquareWidth == value) return;
                _innerSquareWidth = value;
                Invalidate();
            }
        }
        private int _innerSquareHeight = 22;
        [Description("内正方形高"), Category("TF.UI")]
        [DefaultValue(22)]
        public int InnerSquareHeight
        {
            get => _innerSquareHeight;
            set
            {
                if (_innerSquareHeight == value) return;
                _innerSquareHeight = value;
                Invalidate();
            }
        }
        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            KeystrokeClick();
            
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            int w = Width - 1;
            int h = Height - 1;
            var graphics = e.Graphics;
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            // 创建一个圆角矩形路径
            using GraphicsPath path = new GraphicsPath();
            int x = 0; // 圆角正方形的左上角 x 坐标
            int y = 0; // 圆角正方形的左上角 y 坐标
            int width = w; // 圆角长方形的宽度
            int height = h; // 圆角长方形的高度
            DrawRoundedRectangle(graphics, x, y, width, height, Color.FromArgb(252, 252, 252), Color.FromArgb(182, 182, 184));

            x = OuterSpacing; // 圆角正方形的左上角 x 坐标
            y = OuterSpacing; // 圆角正方形的左上角 y 坐标
            width = w - (x << 1); // 圆角长方形的宽度
            height = h - (y << 1); // 圆角长方形的高度
            DrawRoundedRectangle(graphics, x, y, width, height, OuterSpacingColor1, OuterSpacingColor2);

            x += InnerSpacing; // 圆角正方形的左上角 x 坐标
            y += InnerSpacing; // 圆角正方形的左上角 y 坐标
            width = w - (x << 1); // 圆角长方形的宽度
            height = h - (y << 1); // 圆角长方形的高度
            DrawRoundedRectangle(graphics, x, y, width, height, Color.FromArgb(252, 252, 252), Color.FromArgb(182, 182, 184));

            width = InnerSquareWidth; // 圆角长方形的宽度
            height = InnerSquareHeight; // 圆角长方形的高度
            x = w - InnerSquareWidth >> 1; // 圆角正方形的左上角 x 坐标
            y = h - InnerSquareHeight >> 1; // 圆角正方形的左上角 y 坐标
            DrawRoundedRectangle(graphics, x, y, width, height, AnimationColor1, AnimationColor2);

            // 绘制图像
            using Bitmap? bitmap = Keystroke switch
            {
                KeystrokeType.Up => Resources.up,
                KeystrokeType.Down => Resources.down,
                KeystrokeType.Left => Resources.left,
                KeystrokeType.Right => Resources.right,
                _ => null
            };
            if (bitmap != null)
            {
                x += width - ImageSize >> 1;
                y += height - ImageSize >> 1;
                e.Graphics.DrawImage(bitmap, x, y, ImageSize, ImageSize);
            }
            else
            {
                using SolidBrush stringBrush = new SolidBrush(ForeColor);
                string txt = Keystroke.ToString();
                SizeF sizeF = txt.MeasureString(Font);
                x += (int)Math.Round((width - sizeF.Width) / 2);
                y += (int)Math.Round((height - sizeF.Height) / 2);
                e.Graphics.DrawString(txt, Font, stringBrush, x, y);
            }

            base.OnPaint(e);
        }
        private void DrawRoundedRectangle(Graphics graphics, int x, int y, int width, int height, Color start, Color end)
        {
            int cornerRadius = Radius;
            // 创建一个圆角矩形路径
            using GraphicsPath path = new GraphicsPath();
            path.AddArc(x, y, cornerRadius << 1, cornerRadius << 1, 180, 90); // 左上角
            path.AddLine(x + cornerRadius, y, x + width - cornerRadius, y); // 上边线
            path.AddArc(x + width - (cornerRadius << 1), y, cornerRadius << 1, cornerRadius << 1, 270, 90); // 右上角
            path.AddLine(x + width, y + cornerRadius, x + width, y + height - cornerRadius); // 右边线
            path.AddArc(x + width - (cornerRadius << 1), y + height - (cornerRadius << 1), cornerRadius << 1, cornerRadius << 1, 0, 90); // 右下角
            path.AddLine(x + width - cornerRadius, y + height, x + cornerRadius, y + height); // 下边线
            path.AddArc(x, y + height - (cornerRadius << 1), cornerRadius << 1, cornerRadius << 1, 90, 90); // 左下角
            path.AddLine(x, y + height - cornerRadius, x, y + cornerRadius); // 左边线
            path.CloseFigure();

            // 创建线性渐变画刷
            using LinearGradientBrush brush = new LinearGradientBrush(
                new Rectangle(x, y, width, height),
                start,
                end,
                LinearGradientMode.Vertical);

            // 填充圆角矩形
            graphics.FillPath(brush, path);
        }
        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
        }
    }
}
